<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 过滤器作用: 文本格式化 -->
    <!-- 双大括号和v-bind都可以使用过滤器 -->
    <!-- 过滤器还可以串联 -->
    <div id="app">
      <h2>vue2过滤器</h2>
      <!-- <p>性别: {{ sex === 1 ? "男" : "女" }}</p> -->
      <p>性别: {{ sex | fn | fn2 }}</p>
      <p :title="time | format">当前事件: {{ time | format }}</p>
    </div>

    <script src="./lib/vue2.js"></script>
    <script>
      new Vue({
        data() {
          return {
            sex: 2,
            time: 1684218547924,
          };
        },
        filters: {
          fn(val) {
            return val === 1 ? "男" : "女";
          },
          fn2(val) {
            return val === "男" ? "♂" : "♀";
          },
          format(val) {
            let time = new Date(val);
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let date = time.getDate();
            let hours = time.getHours();
            let minute = time.getMinutes();
            let second = time.getSeconds();

            if (month < 10) {
              month = "0" + month;
            }
            if (date < 10) {
              date = "0" + date;
            }
            if (hours < 10) {
              hours = "0" + hours;
            }
            if (minute < 10) {
              minute = "0" + minute;
            }
            if (second < 10) {
              second = "0" + second;
            }
            return (
              year +
              "-" +
              month +
              "-" +
              date +
              " " +
              hours +
              ":" +
              minute +
              ":" +
              second
            );
          },
        },
        // mounted() {
        //   console.log(new Date().getTime());
        // },
      }).$mount("#app");
    </script>
  </body>
</html>
